package com.billy.ohos.swipe.demo.consumer;

import com.billy.ohos.swipe.*;
import com.billy.ohos.swipe.consumer.DrawerConsumer;
import com.billy.ohos.swipe.consumer.SlidingConsumer;
import com.billy.ohos.swipe.consumer.StretchConsumer;
import com.billy.ohos.swipe.demo.BaseRecyclerViewAbility;
import com.billy.ohos.swipe.demo.BounceInterpolator;
import com.billy.ohos.swipe.demo.ResourceTable;
import com.ryan.ohos.extension.scroller.animation.Interpolator;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import java.util.Arrays;


/**
 * demo:
 *
 * @author billy.qi
 */
public class DrawerConsumerAbility extends BaseRecyclerViewAbility {

    @Override
    public int getTitleResId() {
        return ResourceTable.String_demo_ui_DrawerConsumer;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        smartSwipeRefresh = SmartSwipeRefresh.scaleMode(findComponentById(ResourceTable.Id_container_refresh), false)
                .setDataLoader(dataLoader);


        SwipeConsumer consumer = smartSwipeRefresh.getSwipeConsumer().as(SlidingConsumer.class)
                .setDrawerExpandable(false)
                .setEdgeAffinity(true)
                .addConsumer(new StretchConsumer())
                .enableVertical();
        consumer.getWrapper().setTag("DrawerConsumerAbility");
    }

    SwipeConsumerExclusiveGroup exclusiveGroup = new SwipeConsumerExclusiveGroup();

    @Override
    protected ViewHolder createRecyclerViewHolder(Component view) {
        Text textView = new Text(DrawerConsumerAbility.this);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(0xFFAA0000));
        textView.setBackground(element);

        textView.setTextColor(new Color(0xFFFFFFFF));
        textView.setTextSize(SmartSwipe.dp2px(14, getContext()));
        textView.setMultipleLine(true);
        textView.setPadding(50 , 0 ,0 , 0);
//        textView.setTextAlignment(TextAlignment.CENTER);
        textView.setLayoutConfig(new StackLayout.LayoutConfig(500, StackLayout.LayoutConfig.MATCH_PARENT));
        itemConsumer = SmartSwipe.wrapView(view)
                .addConsumer(new DrawerConsumer())
                .setHorizontalDrawerView(textView)
                .setScrimColor(0x2F000000)
                .setShadowColor(0x80000000)
                .addToExclusiveGroup(exclusiveGroup);
        return new ViewHolder(itemConsumer.getWrapper(), textView, itemConsumer);
    }

    static Interpolator[] INTERPOLATORS = null;

    static {
        INTERPOLATORS = Arrays.asList(
//                    new AccelerateInterpolator()
//                    , new AccelerateDecelerateInterpolator()
//                    , new DecelerateInterpolator()
//                    , new LinearInterpolator()
//                    , new AnticipateInterpolator()
//                    , new AnticipateOvershootInterpolator()
//                    , new OvershootInterpolator()
                new BounceInterpolator()
        ).toArray(new Interpolator[0]);
    }

    class ViewHolder extends BaseRecyclerViewAbility.ViewHolder {
        private final Text textView;
//        private int position;
        private SwipeConsumer mConsumer;

        ViewHolder(SmartSwipeWrapper wrapper, Text textView, SwipeConsumer consumer) {
            super(wrapper);
            this.textView = textView;
            textView.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
//                    Toast.makeText(DrawerConsumerAbility.this, "position:(" + position + ") drawer clicked!", Toast.LENGTH_SHORT).show();
                }
            });
            this.mConsumer = consumer;
        }

        @Override
        protected void onBindData(BaseRecyclerViewAbility.Data data, int position) {
            super.onBindData(data, position);
            textView.setText(getString(ResourceTable.String_demo_drawer_ui_item_text));
            Interpolator interpolator = INTERPOLATORS[position % INTERPOLATORS.length];
            mConsumer.setInterpolator(interpolator);
            textView.setText(getString(ResourceTable.String_demo_drawer_ui_item_text, interpolator.getClass().getSimpleName()));
//            this.position = position;
        }

    }

    @Override
    protected int getMessageArray() {
        return ResourceTable.Strarray_demo_drawer_ui_messages;
    }
}
